#!/usr/bin/perl
use strict;
use Spreadsheet::WriteExcel::Big;
use List::Util ('sum');

my %all_ks;
while (<>)
{
	chomp;
	my ($ks, $type) = split /\t/;
	push(@{$all_ks{$type}}, $ks * 2);
}
my $average_len = sum(@{$all_ks{'Mouse Gene'}}, @{$all_ks{'Rat Gene'}})
		/ (@{$all_ks{'Mouse Gene'}} + @{$all_ks{'Rat Gene'}});
print STDERR $average_len, "\n";
for my $type("None",
			 "Mouse Gene", "Rat Gene",
			 "Mouse node", "Rat node",
			 "Orth",
			 "Mouse Orphan", "Rat Orphan",
			 "Ancestral node")
{
	if($type =~/Gene/)
	{
		$_ = 0 for (@{$all_ks{$type}});
		print $type, "\n", @{$all_ks{$type}} + 0, " genes\n\n";

	}
	else
	{
		$_ -= $average_len for (@{$all_ks{$type}});
		@{$all_ks{$type}} = sort {$a <=> $b} (@{$all_ks{$type}});
		print $type, "\n", join "\n", @{$all_ks{$type}}, "\n";
	}
}
my $workbook  = Spreadsheet::WriteExcel::Big->new('ks_tree.xls');
die "Problems creating new Excel file: $!" unless defined $workbook;
		
#formats
my $bold_format = $workbook->addformat();
$bold_format->set_bold();
$bold_format->set_color('red');
$bold_format->set_align('right');
my $right_format = $workbook->addformat();
$right_format->set_align('right');
my $percent_format = $workbook->addformat();
$percent_format->set_align('right');
$percent_format->set_num_format('0.0%');
my $bins_format = $workbook->addformat();
$bins_format->set_align('right');
$bins_format->set_align('right');
$bins_format->set_color('blue');
$bins_format->set_num_format('0.00;0.00');
{
	my $worksheet = $workbook->addworksheet("Ks_values");
	$worksheet->set_column(0,8,15);
	my $col = 0;
	for my $type("Mouse node", "Rat node",
				 "Orth",
				 "Mouse Orphan", "Rat Orphan",
				 "Ancestral node")
	{
		$worksheet->write(0, $col, $type, $bold_format);
		$worksheet->write_col(1, $col, $all_ks{$type}, $right_format);
		$col++;
	}
}
# bin
my $binsize = 0.02;
my $bincnt  = 201;
my $offset = 7;
{
	my $worksheet = $workbook->addworksheet("Binned Ks_values");
	$worksheet->set_column(0,15,14);
	my $col = 0;
	my @bins = reverse (0..($bincnt - 1));
	$_ *= -$binsize for (@bins);

	$worksheet->write(0, $col, 'count', $bold_format);
	$worksheet->write(1, $col, 'bin', $bold_format);
	$worksheet->write_col(2, $col, \@bins, $bins_format);
	$worksheet->write(1, $col+$offset, 'bin', $bold_format);
	$worksheet->write_col(2, $col+$offset, \@bins, $bins_format);
	$col++;
	for my $type("Mouse node", "Rat node",
				 "Orth",
				 "Mouse Orphan", "Rat Orphan",
				 "Ancestral node")
	{
		$worksheet->write(0, $col, @{$all_ks{$type}} + 0, $right_format);
		$worksheet->write(1, $col, $type, $bold_format);
		$worksheet->write(1, $col+$offset, $type, $bold_format);
		my @ks = (0) x $bincnt;
		for my $ks(@{$all_ks{$type}})
		{
			$ks[$ks / -$binsize]++;
		}
		@ks = reverse @ks[0..($bincnt-1)];
		$worksheet->write_col(2, $col, \@ks, $right_format);
		my $total_cnt = @{$all_ks{$type}};
		$_ /= $total_cnt for (@ks);
		$worksheet->write_col(2, $col+$offset, \@ks, $percent_format);
		$col++;
	}
}
$workbook->close();


__DATA__



